#include  <cstdio>

int a = 23, b = 28, c = 33,x,y,z;

void init()
{
	x = y = z = 1;
	while( 33*28*x % 23 != 1) x++;
	while( 23*33*y % 28 != 1) y++;
	while( 23*28*z % 33 != 1) z++;
}

int main(int argc, char *argv[])
{
	init();
	int p,e,i,d,counter = 0;
	while( scanf("%d%d%d%d",&p,&e,&i,&d) && p != -1 )
	{
		int sum = 21252 + 924*x*p + 759*y*e + 644*z*i;
		sum -= d;
		while(sum > 21252) sum -= 21252;
		printf("Case %d: the next triple peak occurs in %d days.\n",
				++counter,sum);
	}
}
